home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap16 / SysPal3 / SysPal3.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.6 KB  |  128 lines

  1. /*----------------------------------------
  2.    SYSPAL3.C -- Displays system palette
  3.                 (c) Charles Petzold, 1998
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. TCHAR szAppName [] = TEXT ("SysPal3") ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14. {
  15.      HWND     hwnd ;
  16.      MSG      msg ;
  17.      WNDCLASS wndclass ;
  18.  
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = NULL ;
  28.      wndclass.lpszClassName = szAppName ;
  29.      
  30.      if (!RegisterClass (&wndclass))
  31.      {
  32.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  33.                       szAppName, MB_ICONERROR) ;
  34.           return 0 ;
  35.      }
  36.      
  37.      hwnd = CreateWindow (szAppName, TEXT ("System Palette #3"), 
  38.                           WS_OVERLAPPEDWINDOW, 
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.  
  43.      if (!hwnd)
  44.           return 0 ;
  45.  
  46.      ShowWindow (hwnd, iCmdShow) ;
  47.      UpdateWindow (hwnd) ;
  48.  
  49.      while (GetMessage (&msg, NULL, 0, 0))
  50.      {
  51.           TranslateMessage (&msg) ;
  52.           DispatchMessage (&msg) ;
  53.      }
  54.      return msg.wParam ;
  55. }
  56.  
  57. BOOL CheckDisplay (HWND hwnd)
  58. {
  59.      HDC hdc ;
  60.      int iPalSize ;
  61.  
  62.      hdc = GetDC (hwnd) ;
  63.      iPalSize = GetDeviceCaps (hdc, SIZEPALETTE) ;
  64.      ReleaseDC (hwnd, hdc) ;
  65.  
  66.      if (iPalSize != 256)
  67.      {
  68.           MessageBox (hwnd, TEXT ("This program requires that the video ")
  69.                             TEXT ("display mode have a 256-color palette."),
  70.                       szAppName, MB_ICONERROR) ;
  71.           return FALSE ;
  72.      }
  73.      return TRUE ;
  74. }
  75.  
  76. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  77. {
  78.      static HBITMAP hBitmap ;
  79.      static int     cxClient, cyClient ;
  80.      BYTE           bits [256] ;
  81.      HDC            hdc, hdcMem ;
  82.      int            i ;
  83.      PAINTSTRUCT    ps ;
  84.  
  85.      switch (message)
  86.      {
  87.      case WM_CREATE:
  88.           if (!CheckDisplay (hwnd))
  89.                return -1 ;
  90.  
  91.           for (i = 0 ; i < 256 ; i++)
  92.                bits [i] = i ;
  93.           
  94.           hBitmap = CreateBitmap (16, 16, 1, 8, &bits) ;
  95.           return 0 ;
  96.                                                         
  97.      case WM_DISPLAYCHANGE:
  98.           if (!CheckDisplay)
  99.                DestroyWindow (hwnd) ;
  100.  
  101.           return 0 ;
  102.  
  103.      case WM_SIZE:
  104.           cxClient = LOWORD (lParam) ;
  105.           cyClient = HIWORD (lParam) ;
  106.           return 0 ;
  107.  
  108.      case WM_PAINT:
  109.           hdc = BeginPaint (hwnd, &ps) ;
  110.  
  111.           hdcMem = CreateCompatibleDC (hdc) ;
  112.           SelectObject (hdcMem, hBitmap) ;
  113.  
  114.           StretchBlt (hdc,    0, 0, cxClient, cyClient,
  115.                       hdcMem, 0, 0, 16, 16, SRCCOPY) ;
  116.  
  117.           DeleteDC (hdcMem) ;
  118.           EndPaint (hwnd, &ps) ;
  119.           return 0 ;
  120.  
  121.      case WM_DESTROY:
  122.           DeleteObject (hBitmap) ;
  123.           PostQuitMessage (0) ;
  124.           return 0 ;
  125.      }
  126.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  127. }
  128.